/**
 * 
 */
package com.gr.staffpm.pages.dashboard.gadgets;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.service.TaskService;


/**
 * @author Graham Rhodes 5 Apr 2011 15:05:20
 */
public class TasksDueGadget extends Panel {

    private static final long serialVersionUID = 1L;
    @SpringBean(name = "taskService")
    private TaskService taskService;
    @SpringBean(name = "userService")
    private UserService userService;

    public TasksDueGadget(String id) {
        super(id);

        boolean empty = true;

        Label noTasksLabel = new Label("noTasksLabel", "You have no tasks due in the next 30 days. :D");

        RepeatingView tasks = new RepeatingView("tasks");

        DateTime dueDate = new DateTime();
        List<Task> taksDue = taskService.getUnresolvedUserTasksOnDate(userService.getCurrentUser(), dueDate);
        for (int count = 0, i = 0; i < 31; i++) {
            if (taksDue != null && taksDue.size() > 0) {
                tasks.addOrReplace(new TasksDuePanel(String.valueOf(i), dueDate, new ArrayList<Task>(taksDue)));
                count++;
                empty = false;
            }
            dueDate = dueDate.plusDays(1);
            taksDue = taskService.getUnresolvedUserTasksOnDate(userService.getCurrentUser(), dueDate);
            if (count == 10)
                break;
        }
        noTasksLabel.setVisible(empty);
        add(noTasksLabel);
        add(tasks);
    }
}
